package com.cl.short_video_java.util;

import com.cl.short_video_java.pojo.SysLogininfor;
import com.cl.short_video_java.pojo.SysMailLog;
import com.cl.short_video_java.pojo.SysOperLog;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 日志导出
 */
public class ExceUtil {
    /**
     * sys_logininfor
     * 登录日志
     * @param sysLogininfors
     * @return
     * @throws IOException
     */
    public static ByteArrayOutputStream exportSysLogininforExcel(List<SysLogininfor> sysLogininfors) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Sheet sheet = workbook.createSheet("登录日志");
        // 创建表头
        Row headerRow = sheet.createRow(0);
        Cell userCell = headerRow.createCell(0);
        userCell.setCellValue("用户");
        Cell ipCell = headerRow.createCell(1);
        ipCell.setCellValue("IP地址");
        Cell addrCell = headerRow.createCell(2);
        addrCell.setCellValue("登录地点");
        Cell osCell = headerRow.createCell(3);
        osCell.setCellValue("操作系统");
        Cell statusCell = headerRow.createCell(4);
        statusCell.setCellValue("登录状态");
        Cell loginCell= headerRow.createCell(5);
        loginCell.setCellValue("访问时间");
        // 填充数据
        int rowNum = 1;
        for (SysLogininfor sysLogininfor : sysLogininfors) {
            Row dataRow = sheet.createRow(rowNum++);
            Cell dataUserCell = dataRow.createCell(0);
            dataUserCell.setCellValue(sysLogininfor.getUserName());
            Cell dataIpCell = dataRow.createCell(1);
            dataIpCell.setCellValue(sysLogininfor.getIpaddr());
            Cell dataaddrCell = dataRow.createCell(2);
            dataaddrCell.setCellValue(sysLogininfor.getLoginLocation());
            Cell dataosCell = dataRow.createCell(3);
            dataosCell.setCellValue(sysLogininfor.getOs());
            Cell datastatusCell = dataRow.createCell(4);
            datastatusCell.setCellValue(sysLogininfor.getStatus());
            Cell dataloginCell = dataRow.createCell(5);
            dataloginCell.setCellValue(sysLogininfor.getLoginTime()==null?null:simpleDateFormat.format(sysLogininfor.getLoginTime()));
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();
        return outputStream;
    }

    /**
     * sys_operlog
     * 操作日志
     * @param sysOperLogs
     * @return
     * @throws IOException
     */
    public static ByteArrayOutputStream exportSysOperLogExcel(List<SysOperLog> sysOperLogs) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Sheet sheet = workbook.createSheet("操作日志");
        // 创建表头
        Row headerRow = sheet.createRow(0);
        Cell operId = headerRow.createCell(0);
        operId.setCellValue("日志主键");
        Cell title = headerRow.createCell(1);
        title.setCellValue("模块标题");
        Cell businessType = headerRow.createCell(2);
        businessType.setCellValue("业务类型（0其它 1新增 2修改 3删除）");
        Cell method = headerRow.createCell(3);
        method.setCellValue("方法名称");
        Cell requestMethod = headerRow.createCell(4);
        requestMethod.setCellValue("请求方式");
        Cell operatorType= headerRow.createCell(5);
        operatorType.setCellValue("操作类别（0其它 1后台用户 2手机端用户）");
        Cell operName = headerRow.createCell(6);
        operName.setCellValue("操作人员");
        Cell operUrl = headerRow.createCell(7);
        operUrl.setCellValue("请求URL");
        Cell operIp = headerRow.createCell(8);
        operIp.setCellValue("主机地址");
        Cell operParam = headerRow.createCell(9);
        operParam.setCellValue("请求参数");
        Cell jsonResult = headerRow.createCell(10);
        jsonResult.setCellValue("返回参数");
        Cell status = headerRow.createCell(11);
        status.setCellValue("操作状态（0正常 1异常）");
        Cell errorMsg = headerRow.createCell(12);
        errorMsg.setCellValue("错误消息");
        Cell costTime = headerRow.createCell(13);
        costTime.setCellValue("消耗时间");
        // 填充数据
        int rowNum = 1;
        for (SysOperLog sysOperLog : sysOperLogs) {
            Row dataRow = sheet.createRow(rowNum++);
            Cell dataoperIdCell = dataRow.createCell(0);
            dataoperIdCell.setCellValue(sysOperLog.getOperId());
            Cell datatitleCell = dataRow.createCell(1);
            datatitleCell.setCellValue(sysOperLog.getTitle());
            Cell databusinessTypeCell = dataRow.createCell(2);
            databusinessTypeCell.setCellValue(sysOperLog.getBusinessType());
            Cell datamethodCell = dataRow.createCell(3);
            datamethodCell.setCellValue(sysOperLog.getMethod());
            Cell datarequestMethodsCell = dataRow.createCell(4);
            datarequestMethodsCell.setCellValue(sysOperLog.getRequestMethod());
            Cell dataoperatorTypeCell = dataRow.createCell(5);
            dataoperatorTypeCell.setCellValue(sysOperLog.getOperatorType());
            Cell dataoperNameCell = dataRow.createCell(6);
            dataoperNameCell.setCellValue(sysOperLog.getOperName());
            Cell dataoperUrlCell = dataRow.createCell(7);
            dataoperUrlCell.setCellValue(sysOperLog.getOperUrl());
            Cell dataoperIpCell = dataRow.createCell(8);
            dataoperIpCell.setCellValue(sysOperLog.getOperIp());
            Cell dataoperParamCell = dataRow.createCell(9);
            dataoperParamCell.setCellValue(sysOperLog.getOperParam());
            Cell datajsonResultCell = dataRow.createCell(10);
            datajsonResultCell.setCellValue(sysOperLog.getJsonResult());
            Cell datastatusCell = dataRow.createCell(11);
            datastatusCell.setCellValue(sysOperLog.getStatus());
            Cell dataerrorMsgCell = dataRow.createCell(12);
            dataerrorMsgCell.setCellValue(sysOperLog.getErrorMsg());
            Cell datacostTimeCell = dataRow.createCell(13);
            datacostTimeCell.setCellValue(sysOperLog.getCostTime()+"毫秒");
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();
        return outputStream;
    }

    /**
     * sys_maillog
     * 系统日志
     * @param sysMailLogList
     * @return
     * @throws IOException
     */
    public static ByteArrayOutputStream exportSysMailLogExcel(List<SysMailLog> sysMailLogList) throws IOException {
        System.out.println(sysMailLogList);
        Workbook workbook = new XSSFWorkbook();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Sheet sheet = workbook.createSheet("邮件日志");
        // 创建表头
        Row headerRow = sheet.createRow(0);
        Cell Id = headerRow.createCell(0);
        Id.setCellValue("日志编号");
        Cell subject = headerRow.createCell(1);
        subject.setCellValue("主题");
        Cell content = headerRow.createCell(2);
        content.setCellValue("内容");
        Cell createTime = headerRow.createCell(3);
        createTime.setCellValue("时间");
        Cell userId = headerRow.createCell(4);
        userId.setCellValue("接收账号");
        Cell sendMail= headerRow.createCell(5);
        sendMail.setCellValue("发送人邮箱");
        Cell receptionMail = headerRow.createCell(6);
        receptionMail.setCellValue("接收邮箱");
        // 填充数据
        int rowNum = 1;
        for (SysMailLog sysMailLog : sysMailLogList) {
            Row dataRow = sheet.createRow(rowNum++);
            Cell dataIdCell = dataRow.createCell(0);
            dataIdCell.setCellValue(sysMailLog.getId()==null?"null":sysMailLog.getId().toString());
            Cell datasubjectCell = dataRow.createCell(1);
            datasubjectCell.setCellValue(sysMailLog.getSubject());
            Cell datacontentCell = dataRow.createCell(2);
            datacontentCell.setCellValue(sysMailLog.getContent());
            Cell datacreateTimeCell = dataRow.createCell(3);
            datacreateTimeCell.setCellValue(sysMailLog.getTime()==null?null:simpleDateFormat.format(sysMailLog.getTime()));
            Cell datauserIdCell = dataRow.createCell(4);
            datauserIdCell.setCellValue(sysMailLog.getUserId()==null?"null":sysMailLog.getUserId().toString());
            Cell datasendMailCell = dataRow.createCell(5);
            datasendMailCell.setCellValue(sysMailLog.getSendMail());
            Cell datareceptionMailCell = dataRow.createCell(6);
            datareceptionMailCell.setCellValue(sysMailLog.getReceptionMail());
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();
        return outputStream;
    }
}
